package handler

import (
	"2302a/testyuekao/api/request"
	"2302a/testyuekao/api/resp"
	"2302a/testyuekao/internal/dao"
	"2302a/testyuekao/internal/model"
	"github.com/gin-gonic/gin"
	"time"
)

func AddBullet(c *gin.Context) {
	var req request.AddBulletReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 100000, "参数信息错误", err.Error())
		return
	}
	var user model.Users
	userId := c.GetUint("userId")
	dao.GetOneById(userId, &user)
	if user.UType != 1 {
		resp.Error(c, 100002, "该权限不足", nil)
		return
	}
	var house model.Houses
	dao.GetOneById(req.HouseId, &house)
	if house.Id == 0 {
		resp.Error(c, 100003, "房源信息不存在", nil)
		return
	}
	parse, _ := time.Parse(time.DateTime, req.BarrageTime)

	bullet := model.Bullets{
		UserId:      userId,
		HouseId:     req.HouseId,
		Modes:       req.Modes,
		Text:        req.Text,
		BarrageTime: parse,
		Size:        req.Size,
		Color:       req.Color,
	}
	dao.Create(&bullet)
	resp.Success(c, "弹幕发送成功", true)
}
